package com.mall.goods.adapter.in.web.controller;


import com.mall.goods.adapter.in.web.dto.GoodsCommonBanJsonDTO;
import com.mall.goods.adapter.in.web.dto.GoodsCommonPassJsonDTO;
import com.mall.goods.common.Result;
import com.mall.goods.common.dto.DtGrid;
import com.mall.goods.common.exception.BusinessException;
import com.mall.goods.common.helper.AdminLogHelper;
import com.mall.user.domain.port.in.GoodsUserCase;
import com.mall.goods.common.exception.DtGridException;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品控制器
 */
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsJsonController {
    @Resource
    private GoodsUserCase goodsService;

    @Resource
    private AdminLogHelper adminLogHelper;


    /**
     * 商品列表JSON数据
     *
     * @param dtGridPager 分页参数
     * @return DtGrid 分页数据
     * @author zqk
     * @date 2025/11/6
     */
    @RequestMapping(value = "/list.json", method = RequestMethod.POST)
    public DtGrid listJson(@RequestBody String dtGridPager) throws DtGridException {
        if (StringUtils.isBlank(dtGridPager)) {
            throw new DtGridException("参数不能为空");
        }
        return goodsService.listJson(dtGridPager);
    }


    /**
     * 商品禁售
     *
     * @param goodsCommonBanJsonDTO 商品禁售参数
     * @author yangfei
     */
    //无需将传入的属性封装成对象使用，需要将属性转换成领域模型进行判断
    @PostMapping("/ban.json")
    public Result<Object> banJson(@RequestBody GoodsCommonBanJsonDTO goodsCommonBanJsonDTO) {
        log.info("商品禁售");
        try {
            goodsService.banJson(goodsCommonBanJsonDTO.getCommonId(), goodsCommonBanJsonDTO.getStateRemark());
            adminLogHelper.log("商品禁售,商品id:{}"+goodsCommonBanJsonDTO.getCommonId());
            return Result.success();
        } catch (BusinessException e) {
            return Result.fail(e.getResp());
        }

    }
    /**
     * 商品通过审核
     * @return
     * @author yangfei
     */
    @PostMapping("/pass.json")
    public Result<Object> passJson(@RequestBody GoodsCommonPassJsonDTO goodsCommonPassJsonDTO){
        log.info("商品通过审核接口");
        try{
            goodsService.passJson(goodsCommonPassJsonDTO.getCommonId());
            adminLogHelper.log("商品通过审核,商品id:{}"+goodsCommonPassJsonDTO.getCommonId());
            return Result.success();
        }catch (BusinessException e){
            return Result.fail(e.getResp());
        }

    }


//

}